ohi logo
OHI Science | Citation policy

Summary

This script calculates trend data for cumulative human impacts as well as individual pressures.


Setup

knitr::opts_chunk$set(fig.width = 6, fig.height = 4, fig.path = 'figs/',message = FALSE, warning = FALSE)

# setwd("trend")

library(tidyr)
library(dplyr)
library(raster)
library(RColorBrewer)
library(tidyverse)
library(rgdal)
library(doParallel)
library(foreach)
library(sf)
library(gstat)
library(stringr)

# load spatial files (ocean raster and regions shapefile)
source("https://raw.githubusercontent.com/OHI-Science/ohiprep_v2018/master/src/R/spatial_common.R")
## Reading layer `regions_2017_update' from data source `/home/shares/ohi/git-annex/globalprep/spatial/v2017' using driver `ESRI Shapefile'
## Simple feature collection with 526 features and 7 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -18040060 ymin: -9020048 xmax: 18040080 ymax: 9020048
## epsg (SRID):    NA
## proj4string:    +proj=moll +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
cols = rev(colorRampPalette(brewer.pal(11, 'Spectral'))(255)) # rainbow color scheme


# paralell processing
cl<-makeCluster(10)
registerDoParallel(cl)
chi   <- list.files(file.path(dir_M,'git-annex/impact_acceleration/impact/cumulative_impact'), pattern='chi', full.names=TRUE)

chi_stack <- stack(chi)

samp_n <- 100

rand_samp <- sampleRandom(chi_stack, size=samp_n) %>%
  data.frame()

rand_samp$sample_id <- 1:samp_n

rand_samp_data <- rand_samp %>%
  gather("year", "chi", starts_with("chi")) %>%
  mutate(year = substr(year, 5, 8)) %>%
  mutate(year = as.numeric(year))
   
# check that everything went well
summary(rand_samp_data)        
table(rand_samp_data$sample_id)  

write.csv(rand_samp_data, "int/rand_samp.csv", row.names=FALSE)

Generate plots of each site over time

samp_n <- 100

rand_samp_data <- read.csv("int/rand_samp.csv")


for(i in 1:samp_n){ # i = 2
 
plot_chi <- ggplot(dplyr::filter(rand_samp_data, sample_id==i), aes(y = chi, x = year)) +
    geom_point(size = 2) +
   geom_line() + 
   stat_smooth(method=lm, se=FALSE, color="red")
  
plot(plot_chi)  
}